package com.loozb.thread6;

/**
 * 使用3个线程，实现从0一次交替打印到100
 */
public class Main1 implements Runnable{
    static int threadCount = 4;
    static int maxCount = 100;
    private static final Object LOCK = new Object();
    private int threadNo;

    private static int current = 0;
    public Main1(int threadNo) {
        this.threadNo = threadNo;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (LOCK) {
                //如果是当前线程，给current++
                while (current % threadCount != threadNo) {
                    if(current > maxCount) {
                        break;
                    }
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(current > maxCount) {
                    break;
                }
                System.out.println("thread:" + threadNo + "：" + current);
                current++;

                LOCK.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        for(int i = 0; i < threadCount; i++) {
            new Thread(new Main1(i)).start();
        }
    }
}
